package com.example.mapper;

import com.example.demo.dto.AuthLogDTO;
import com.example.demo.entity.AuthLog;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AuthLogMapper {
    @Select("<script>" +
            "SELECT * FROM `auth-log` " +
            "<where>" +
            "<if test='filter == \"lastMonth\"'>" +
            " smsTime >= NOW() - INTERVAL 1 MONTH" +
            "</if>" +
            "<if test='filter == \"lastWeek\"'>" +
            " smsTime >= NOW() - INTERVAL 7 day" +
            "</if>" +
            "</where>" +
            " LIMIT #{pageNum}, #{pageSize}" +
            "</script>")
    List<AuthLog> findByFilter(@Param("filter") String filter, @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    @Select("<script>" +
            "SELECT count(*) FROM `auth-log` " +
            "<where>" +
            "<if test='filter == \"lastMonth\"'>" +
            "   smsTime >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)" +
            "</if>" +
            "<if test='filter == \"lastWeek\"'>" +
            "   smsTime >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK)" +
            "</if>" +
            "</where>" +
            "</script>")
    long countByFilter(@Param("filter") String filter);

    @Select("SELECT * FROM `auth-log`")
    List<AuthLog> findAll();

    @Select("SELECT count(*) FROM `auth-log`")
    long countAll();

    @Select("SELECT `name`, phone, `num` AS job_num, `smsTime` AS smsTime, `type` FROM `auth-log`")
    List<AuthLogDTO> findAllAuthLogs();

    @Update("UPDATE `auth-log` SET `type` = #{type} WHERE `num` = #{job_num}")
    int updateType(@Param("job_num") String jobNum, @Param("type") String type);

    @Insert("INSERT INTO `auth-log` (name, phone, num, smsTime, type) VALUES (#{name}, #{phone}, #{num}, #{smsTime}, #{type})")
    int insertLog(AuthLog authLog);
}
